package com.zys.service.impl;

import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import com.zys.pojo.Good;
import com.zys.dao.GoodDao;
import com.zys.entity.QueryInfo;
import com.zys.entity.PageResult;
import com.github.pagehelper.Page;
import com.zys.service.GoodService;
import com.github.pagehelper.PageHelper;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GoodServiceImpl implements GoodService {

  @Autowired
  private GoodDao goodDao;

  @Override
  public PageResult<Good> getGoodsList(QueryInfo queryInfo) {
    PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
    Page<Good> page = goodDao.findByCondition(queryInfo.getCondition());
    return new PageResult<>(page.getTotal(), page.getResult());
  }

  @Override
  public void add(Integer id, Good good) {
    good.setAddTime(new Date().getTime());
    goodDao.add(good);
    Integer goodId = good.getId();

    Map<String, Integer> map = new HashMap<>();
    map.put("goodId", goodId);
    map.put("cateId", id);
    goodDao.setRelation4GoodsAndCategory(map);

  }

  @Override
  public void delete(Integer id) {
    goodDao.rmRelation4GoodsAndCategory(id);
    goodDao.delete(id);
  }

  @Override
  public Good findGoodById(Integer id) {
    return goodDao.findGoodById(id);
  }

  @Override
  public void edit(Good good) {
    goodDao.edit(good);
  }
}
